const Koa = require('koa');
const path = require('path');
const {sep} = path;
const middleware = require('./loader/middleware');
const router = require('./loader/router');
const controller = require('./loader/controller');
const service = require('./loader/service');
const routerSchema = require('./loader/router-schema');
const extend = require('./loader/extend');
const config = require('./loader/config');
const env = require('./env')
module.exports={
  start(options={}){
    const app = new Koa()
    app.env = env()
    app.options = options
    app.baseDir = process.cwd()
    app.businessPath = path.resolve(app.baseDir,`.${sep}app`)
    config(app)
    middleware(app)
    routerSchema(app)
    controller(app)
    service(app)
    extend(app)
    try {
      require(path.resolve(app.businessPath,`.${sep}middleware.js`))(app)
    } catch (error) {
      console.log(error)
    }
    router(app)
    try{
      const port = process.env.PORT || 8080
      const host = process.env.IP || '0.0.0.0'
      app.listen(port,host)
    }catch(e){
      console.log(e)
    }

  }
}